﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Text;
using System.Data;
using Maticsoft.Common;
using System.Drawing;

namespace Admin.Finance.SubInvoicePayMent
{
    public partial class AllList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        #region gridView
        public void BindData()
        {
            #region
            //if (!Context.User.Identity.IsAuthenticated)
            //{
            //    return;
            //}
            //AccountsPrincipal user = new AccountsPrincipal(Context.User.Identity.Name);
            //if (user.HasPermissionID(PermId_Modify))
            //{
            //    gridView.Columns[6].Visible = true;
            //}
            //if (user.HasPermissionID(PermId_Delete))
            //{
            //    gridView.Columns[7].Visible = true;
            //}
            #endregion
            StringBuilder strWhere = new StringBuilder();


            if (txtMinSubInvoiceNumber.Text.Trim()!=""&&txtMaxSubInvoiceNumber.Text.Trim()!="")
            {
                strWhere.AppendFormat(" and InvoiceNumber between '{0}' and '{1}'", txtMinSubInvoiceNumber.Text.Trim(),txtMaxSubInvoiceNumber.Text.Trim());
            }
            else if (txtMinSubInvoiceNumber.Text.Trim() != "" && txtMaxSubInvoiceNumber.Text.Trim() == "")
            {
                strWhere.AppendFormat(" and InvoiceNumber>'{0}'",txtMinSubInvoiceNumber.Text.Trim());
            }
            else if (txtMinSubInvoiceNumber.Text.Trim() == "" && txtMaxSubInvoiceNumber.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and InvoiceNumber<'{0}'",txtMaxSubInvoiceNumber.Text.Trim());
            }
            if (txtInvoiceReceiveMan.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and InvoiceReceiveMan like '%{0}%'", txtInvoiceReceiveMan.Text.Trim());
            }
            if (txtPayMan.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and PayMan like '%{0}%'" + txtPayMan.Text.Trim());
            }
            if (txtReceiveBeginDate.Value != "" && txtReceiveEndDate.Value != "")
            {
                strWhere.AppendFormat(" and PayDate between '" + txtReceiveBeginDate.Value + "' and '" + txtReceiveEndDate.Value + "'");
            }
            else if (txtReceiveBeginDate.Value != "" && txtReceiveEndDate.Value == "")
            {
                strWhere.AppendFormat(" and PayDate>'" + txtReceiveBeginDate.Value + "'");
            }
            else if (txtReceiveBeginDate.Value == "" && txtReceiveEndDate.Value != "")
            {
                strWhere.AppendFormat(" and PayDate<'" + txtReceiveEndDate.Value + "'");
            }

            try
            {
                ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();

                DataSet ds = bllBind.GetDataSet("SUM(PayMoney) as SumPayMoney", "V_FinaceSubPayMentList", strWhere.ToString(), true);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    lblPayCount.Text = ds.Tables[0].Rows[0]["SumPayMoney"].ToString();
                }
                ChinaSoft.BLL.Common.Pager.BindGridView(gridView, AspNetPager1, "V_FinaceSubPayMentList", "SubInvoicePaymentId", "*", strWhere.ToString(), "CreateDate desc", "");
            }
            catch (Exception ex)
            {
                Response.Write("数据库错误：" + ex.Message);
                Response.End();
            }
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }
        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[0].Text = "<input type=\"checkbox\" id=\"cbSelectAll\" name=\"cbSelectAll\" onclick=\"javascript:checkJs(this.checked);\" />";
            }
        }
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //LinkButton linkbtnDel = (LinkButton)e.Row.FindControl("LinkButton1");
                //linkbtnDel.Attributes.Add("onclick", "return confirm(\"你确认要删除吗\")");

                //object obj1 = DataBinder.Eval(e.Row.DataItem, "Levels");
                //if ((obj1 != null) && ((obj1.ToString() != "")))
                //{
                //    e.Row.Cells[1].Text = obj1.ToString();
                //}
            }
        }

        #endregion

        protected void btnSearch_Click(object sender, ImageClickEventArgs e)
        {
            BindData();
        }
    }
}